﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SQL_Manager
{
    //TODO: Support more Database drivers (PGSQL can be modelled from TDA sources)
    public partial class frmConnect : Form
    {
        public frmConnect()
        {
            InitializeComponent();
        }

        private void cmdConnect_Click(object sender, EventArgs e)
        {
            using (this)
            {
                try
                {
                    switch ((DriverType)cmbDBType.SelectedIndex)
                    {
                        case DriverType.MYSQL:
                            new Database_Drivers.Driver_MySQL(txtUsername.Text, txtPassword.Text, txtDatabase.Text, txtHost.Text);
                            this.DialogResult = DialogResult.OK;
                            break;
                        case DriverType.PGSQL:
                        case DriverType.SQLITE:
                        default:
                            MessageBox.Show(this, "Invalid or missing driver requested.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1);
                            this.DialogResult = DialogResult.Abort;
                            break;
                    }
                }
                catch (Exception Ex)
                {
                    MessageBox.Show(this, string.Format("Connection error, aborting.\r\n{0}", Ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1);
                    this.DialogResult = DialogResult.Abort;
                }
            }
        }

        private void frmConnect_Load(object sender, EventArgs e)
        {
            this.cmbDBType.SelectedIndex = 0;

            //
            // Populates view with categories and profile names
            //
            foreach (Profile identity in ApplicationProfiles.profiles)
            {
                TreeNode child = new TreeNode(identity.name);
                //
                // Hides the index in the ImageKey, as a string
                //
                child.ImageKey = "" + identity.id;
                //
                // Adds profile to category specified
                // Attempts to locate already made categories with same name to add to
                //
                if (identity.category != String.Empty)
                {
                    if (this.tvProfiles.Nodes[identity.category] == null)
                    {
                        this.tvProfiles.Nodes.Add(new TreeNode(identity.category, new TreeNode[] { child }) { Name = identity.category });
                    }
                    else
                    {
                        this.tvProfiles.Nodes[identity.category].Nodes.Add(child);
                    }
                }
                else
                {
                    this.tvProfiles.Nodes.Add(child);
                }
            }

            //
            // Assigns the context menu to each individual selection
            //
            // This eliminates some little issues that you encounter
            // when just adding context menu to the control.
            //
            foreach (TreeNode it in this.tvProfiles.Nodes)
            {
                // We only want profiles, not categories
                if (it.Nodes.Count == 0)
                {
                    it.ContextMenuStrip = this.ctxProfileMenu;
                }
                else
                {
                    foreach (TreeNode subnode in it.Nodes)
                    {
                        subnode.ContextMenuStrip = this.ctxProfileMenu;
                    }
                }
            }
        }

        //
        // Load the selected property
        //
        // The index is the ImageKey, clever huh?
        //
        private void tvProfiles_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (this.tvProfiles.SelectedNode != null && this.tvProfiles.SelectedNode.Nodes.Count == 0)
            {
                Profile p = (Profile)ApplicationProfiles.profiles[int.Parse(this.tvProfiles.SelectedNode.ImageKey)];
                this.txtUsername.Text = p.username;
                this.txtPassword.Text = p.password;
                this.txtDatabase.Text = p.database;
                this.txtHost.Text = p.hostname;
                this.cmbDBType.SelectedIndex = (int)p.driver;
            }
        }

        private void cmdSaveConnection_Click(object sender, EventArgs e)
        {
            DialogResult _Dr = new frmSaveConnection(this.txtUsername.Text, this.txtPassword.Text, this.txtDatabase.Text, this.txtHost.Text, this.cmbDBType.SelectedIndex).ShowDialog();

            if (_Dr == DialogResult.OK)
            {
                //
                // Reset the profile view and re-populate it
                //
                this.tvProfiles.Nodes.Clear();
                this.frmConnect_Load(null, null);
            }
        }

        private void ctxOptionRemoveProfile_Click(object sender, EventArgs e)
        {
            // TODO: Remove functionality
            MessageBox.Show(this.tvProfiles.SelectedNode.Text);
        }

        private void ctxProfileMenu_Opening(object sender, CancelEventArgs e)
        {
            //(ContextMenu)sender = (ContextMenu)sender;
            
        }

        private void cmdManage_Click(object sender, EventArgs e)
        {
            DialogResult _Dr = new frmManageProfiles().ShowDialog();

            if (_Dr == DialogResult.OK)
            {
                //
                // Reset the profile view and re-populate it
                //
                this.tvProfiles.Nodes.Clear();
                this.frmConnect_Load(null, null);
            }
        }
    }
}
